package com.gk.panda.apis.controller;

import com.alibaba.nacos.common.utils.HttpMethod;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gk.panda.apis.dto.BiologyPointSaveDto;
import com.gk.panda.apis.dto.BiologyPointSearchDto;
import com.gk.panda.apis.dto.BiologyPointSortDto;
import com.gk.panda.apis.log.Log;
import com.gk.panda.apis.service.BiologyPointService;
import com.gk.panda.commons.exception.ServerException;
import com.gk.panda.commons.result.Result;
import com.gk.panda.pojo.BiologyPoint;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

@RestController
@RequestMapping("/edmp/bioPoint")
@Api(tags = "动植物点位")
public class BiologyPointController {

    @Autowired
    private BiologyPointService biologyPointService;

    @ApiOperation(value = "添加动植物点位")
    @Log
    @PostMapping
    public Result<String> addBiologyPoint(@RequestBody BiologyPointSaveDto dto) {
        boolean save = biologyPointService.addBiologyPoint(dto);
        if (save) return Result.write(200,"添加成功");
        return Result.write(201,"添加失败");
    }

    @ApiOperation("分页查看动植物点位列表")
    @PostMapping("/page")
    public Result<Page<BiologyPoint>> getBiologyPointList(@RequestBody BiologyPointSearchDto dto) {
        return Result.ok(biologyPointService.getBiologyPointList(dto));
    }

    @ApiOperation("查看动植物点位详情")
    @GetMapping("/getInfo/{id}")
    public Result<BiologyPoint> getBiologyPointDetails(@PathVariable("id") Integer id) {
        if(null == id){
            throw new ServerException("点位id不能为空");
        }
        return Result.ok(biologyPointService.getBiologyPointDetails(id));
    }

    @ApiOperation("删除动植物点位")
    @Log
    @DeleteMapping("/remove/{id}")
    public Result<String> removePointById(@PathVariable("id") Integer id) {
        boolean remove = biologyPointService.removeById(id);
        if (remove) return Result.write(200,"删除成功");
        return Result.write(201,"删除失败");
    }

    @ApiOperation(value = "修改动植物点位")
    @Log
    @PostMapping("/updateBP")
    public Result<String> updateBP(@RequestBody BiologyPointSaveDto dto) throws UnsupportedEncodingException {
        boolean result = biologyPointService.updateBP(dto);
        if(result){
            return  Result.write(200,"动植物点位修改成功");
        }
        return Result.error(201,"动植物点位修改失败");
    }

    @ApiOperation("动植物点位修改排序")
    @Log
    @PostMapping("/updateSort")
    public Result<String> updateSort(@RequestBody BiologyPointSortDto dto) {
        biologyPointService.updateSort(dto);
        return Result.ok("修改成功");
    }

    @ApiOperation(value = "下载动植物导入模板", httpMethod = HttpMethod.GET)
    @GetMapping("/template")
    public void downloadTemplate(HttpServletResponse response) {
        biologyPointService.downloadTemplate(response);
    }

    @ApiOperation(value = "批量导入动植物信息", httpMethod = HttpMethod.POST)
    @Log
    @PostMapping("/import")
    public Result<String> importBiology(@RequestPart("file") MultipartFile file) {
        String result =  biologyPointService.importBiology(file);
        if (result.equals("success")) {
            return Result.write(200,"导入成功");
        } else if (result.equals("failed")) {
            return Result.write(201,"导入失败");
        } else {
            return Result.write(201,result);
        }
    }

    @ApiOperation(value = "批量导出动植物信息", httpMethod = HttpMethod.GET)
    @Log
    @GetMapping("/export")
    public void exportBiology(HttpServletResponse response) {
        biologyPointService.exportBiology(response);
    }
}
